
package edu.weber.bookstore.database;

import edu.weber.bookstore.util.DatabaseConnection;
import edu.weber.bookstore.util.DatabaseModel;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 * 
 * 
 */
public class UserCart extends DatabaseModel
{
	/**
	 * table tied to the database
	 */
	protected static String TABLE_NAME = "users_carts";
	private String _sessionId;
	private ArrayList<UserCartBook> _userCartBooks;
	/**
	 * Constructor for a session user's shopping cart.
	 * @param sessionId a 32 character string for the user's session
	 */
	public UserCart(String sessionId)
	{
		this(0, sessionId);
	}
	
	/**
	 * Constructor for a session user's shopping cart.
	 * @param id primary key for the UserCart
	 * @param sessionId a 32 character string for the user's session
	 */
	public UserCart(int id, String sessionId)
	{
		this(id, sessionId, new ArrayList<UserCartBook>());
	}
	
	/**
	 * Constructor for a session user's shopping cart.
	 * @param id primary key for the UserCart
	 * @param sessionId a 32 character string for the user's session
	 * @param userCartBooks list of UserCartBooks in the shopping cart
	 */
	public UserCart(int id, String sessionId, ArrayList<UserCartBook> userCartBooks)
	{
		super(id);
		_sessionId = sessionId;
		_userCartBooks = userCartBooks;
	}
	
	/**
	 * Returns the user cart that belongs to the inputed session string or null if not found
	 * @param session_id a 32 character hash for the HttpServletRequest session
	 * @return a UserCart for the given session_id
	 * @throws SQLException
	 */
	public static UserCart find(String session_id) throws SQLException
	{
		UserCart uc = null;
		PreparedStatement pm = DatabaseConnection.getConnection().prepareStatement("SELECT * FROM ".concat(TABLE_NAME).concat(" WHERE session_id = ? LIMIT 1;"));
		pm.setString(1, session_id);
		ResultSet rs = pm.executeQuery();
		
		int id_in = rs.findColumn("id");
		int session_id_in = rs.findColumn("session_id");
		if(rs.next())
		{
			uc = new UserCart(rs.getInt(id_in), rs.getString(session_id_in), UserCartBook.select(rs.getInt(id_in)));
		}
		
		return uc;
	}
	
	@Override
	protected String getTableName()
	{
		return TABLE_NAME;
	}

	@Override
	protected void create() throws SQLException
	{
		PreparedStatement pm = DatabaseConnection.getConnection().prepareStatement("INSERT INTO ".concat(TABLE_NAME).concat(" (session_id) VALUES(?);"), Statement.RETURN_GENERATED_KEYS);
		pm.setString(1, _sessionId);
		
		pm.execute();
		
		setId(DatabaseConnection.lastInsertedId(pm));
		saveUserCartBooks();
	}
	
	@Override
	protected void update() throws SQLException
	{
		PreparedStatement pm = DatabaseConnection.getConnection().prepareStatement("UPDATE ".concat(TABLE_NAME).concat(" SET session_id = ? WHERE id = ? LIMIT 1;"));
		pm.setString(1, _sessionId);
		pm.setInt(2, getId());
		pm.execute();
		
		saveUserCartBooks();
	}
	
	/**
	 * Saves the related objects associated with our cart when we get saved.
	 * @throws SQLException 
	 */
	private void saveUserCartBooks() throws SQLException
	{
		for(int i = 0; i < _userCartBooks.size(); i++)
		{
			UserCartBook ucb = _userCartBooks.get(i);
			ucb.setUserCartId(getId());
			ucb.save();
		}
	}
	
	/**
	 * 
	 * @return 32 character session id associated with the shopping cart.
	 */
	public String getSessionId()
	{
		return _sessionId;
	}
	
	/**
	 * 
	 * @return list of all the UserCartBooks in our shopping cart.
	 */
	public ArrayList<UserCartBook> getUserCartBooks()
	{
		return _userCartBooks;
	}
	
	/**
	 * Sets the session_id associated with this shopping cart.
	 * @param sessionId a 32 character hashed string.
	 */
	public void setSessionId(String sessionId)
	{
		_sessionId = sessionId;
	}
	
	/**
	 * Sets the items in our cart. Does not overwrite what's in the database. You must call delete on the UserCartBook.
	 * @param userCartBooks a nullable array list for items in the user's cart 
	 */
	public void setUserCartBooks(ArrayList<UserCartBook> userCartBooks)
	{
		if(userCartBooks == null)
		{
			userCartBooks = new ArrayList<UserCartBook>();
		}
		_userCartBooks = userCartBooks;
	}
	
	/**
	 * add a single item to our user cart.
	 * @param ucb a nullable UserCartBook that is added to our database.
	 */
	public void addUserCartBook(UserCartBook ucb)
	{
		if(ucb == null)
		{
			return;
		}
		_userCartBooks.add(ucb);
	}
}
